﻿using System.Collections.Generic;
using AgencyManagerSystem.DataFetcher;
using AgencyManagerSystem.Model;
using AgencyManagerSystem.Common;
using System;

namespace AgencyManagerSystem.DataUpdater
{
    public class SupplierMemberUpdater : BaseUpdater<SupplierMemberModel>
    {
        public SupplierMemberUpdater(SupplierMemberFetcher fetcher) : base(fetcher) { }
        public virtual bool UpdateForSupplier(int supplier,IEnumerable<SupplierMemberModel> data,out string message)
        {
            foreach(var item in data)
            {
                item.Supplier = supplier;
            }
            SupplierMemberFetcher supplierlMemberFetcher = new SupplierMemberFetcher();
            var supplierMembers = supplierlMemberFetcher.ListBySupplier(supplier);
            var split = ListSplit(data, supplierMembers);
            foreach(var item in split[0])
            {
                var objId = Save(item, out message);
                if (objId == 0)
                    return false;
            }
            foreach (var item in split[1])
            {
                var objId = Save(item, out message);
                if (objId == 0)
                    return false;
            }
            foreach (var item in split[2])
            {
                var succ = Delete(item, out message);
                if (!succ)
                    return false;
            }
            message = "操作成功";
            return true;
        }
    }
}
